package com.ruoyi.worker.handler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdcardValidatorUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.worker.constant.Constant;
import com.ruoyi.worker.domain.TermWorkerExcel;
import com.ruoyi.worker.service.ITermWorkerService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 长期工导入校验实现类 </p>
 *
 * @author Hush.
 * @since 2021/12/02 14:45
 */
public class TermWorkerExcelVerifyHandlerImpl implements IExcelVerifyHandler<TermWorkerExcel> {


    @Override
    public ExcelVerifyHandlerResult verifyHandler(TermWorkerExcel obj) {
        StringBuilder builder = new StringBuilder();

        //1.获取缓存列表项
        Map<String, Object> cash = SpringUtils.getBean(ITermWorkerService.class).cashDataForTermWorkerExcel();
        //班组信息校验
        if (StringUtils.isNotEmpty(obj.getClazzName())) {
            Map<String, Object> map = (Map<String, Object>) cash.get(Constant.CLAZZ_KEY);
            List<Long> clazzId = new ArrayList<>();
            String[] clazzNameList = obj.getClazzName().split(",");
            for (String s : clazzNameList) {
                Long id = (Long) map.get(s);
                if (StringUtils.isNull(id)) {
                    builder.append("班组:").append(s).append("信息校验失败,");
                } else {
                    clazzId.add(id);
                }
            }

            obj.setClazzId(StringUtils.join(clazzId, ","));
        }
        //区域信息校验
        if (StringUtils.isNotEmpty(obj.getAreaName())) {
            Map<String, Object> map = (Map<String, Object>) cash.get(Constant.AREA_KEY);
            Long id = (Long) map.get(obj.getAreaName());
            if (StringUtils.isNull(id)) {
                builder.append("区域信息校验失败,");
            }
            obj.setAreaId(id);
        }
        //组别信息校验
        if (StringUtils.isNotEmpty(obj.getGroupName())) {
            Map<String, Object> map = (Map<String, Object>) cash.get(Constant.GROUP_KEY);
            Long id = (Long) map.get(obj.getGroupName());
            if (StringUtils.isNull(id)) {
                builder.append("组别信息校验失败,");
            }
            obj.setGroupId(id);
        }
        //岗位信息校验
        if (StringUtils.isNotEmpty(obj.getPostName())) {
            Map<String, Object> map = (Map<String, Object>) cash.get(Constant.POST_KEY);
            Long id = (Long) map.get(obj.getPostName());
            if (StringUtils.isNull(id)) {
                builder.append("岗位信息校验失败,");
            }
            obj.setPostId(id);
        }
        //职级信息校验
        if (StringUtils.isNotEmpty(obj.getProfessionInfo())) {
            Map<String, Object> map = (Map<String, Object>) cash.get(Constant.PROFESSION_KEY);
            Long id = (Long) map.get(obj.getProfessionInfo());
            if (StringUtils.isNull(id)) {
                builder.append("职级信息校验失败,");
            }
            obj.setProfessionId(id);
        }
        //校验员工年纪
        if (StringUtils.isNotEmpty(obj.getIdCard())) {
            if (!IdcardValidatorUtil.isValidate18Idcard(obj.getIdCard())) {
                builder.append("身份证不合法,");
            }
            else {
                Map<String, String> map = DateUtils.getBirthdayAgeSex(obj.getIdCard());
                obj.setBirthday(map.get("birthday"));
                obj.setAge(Long.valueOf(map.get("age")));
                obj.setSex(Integer.valueOf(map.get("sex")));
//                if (obj.getAge() < Constant.AGE_20 || obj.getAge() > Constant.AGE_50) {
//                    builder.append("年龄须在20-50之间,");
//                }
            }
        }
        //校验员工状态
        if (StringUtils.isNotEmpty(obj.getWorkStatusName())){
            Map<String, Object> map = (Map<String, Object>) cash.get(Constant.WORK_STATUS_KEY);
            Integer id = (Integer) map.get(obj.getWorkStatusName());
            if (StringUtils.isNull(id)) {
                builder.append("员工状态校验失败,");
            }else{
                obj.setWorkStatus(id);
            }
        }

        //薪资方式校验
        if (StringUtils.isNotEmpty(obj.getSalaryMethodName())){
            Map<String, Object> map = (Map<String, Object>) cash.get(Constant.SALARY_METHOD_KEY);
            Integer id = (Integer) map.get(obj.getSalaryMethodName());
            if (StringUtils.isNull(id)) {
                builder.append("薪资方式校验失败,");
            }else{
                obj.setSalaryMethod(id);
            }
        }

        //在职状态校验
        if (StringUtils.isNotEmpty(obj.getJobStatusName())){
            Map<String, Object> map = (Map<String, Object>) cash.get(Constant.JOB_STATUS_KEY);
            Integer id = (Integer) map.get(obj.getJobStatusName());
            if (StringUtils.isNull(id)) {
                builder.append("在职状态校验失败,");
            }else{
                obj.setJobStatus(id);
            }
        }

        return new ExcelVerifyHandlerResult(StringUtils.isEmpty(builder), builder.toString());
    }
}
